<template>
  <view v-if="goodsDetailList.goods_name">
    <!-- 轮播图 -->
    <swiper :indicator-dots="true" :autoplay="true" :circular="true" :interval="3000" :duration="1000">
      <swiper-item v-for="(item, index) in goodsDetailList.pics" :key="item.pics_id">
        <image :src="item.pics_big" class="swiper-image" @click="preview(index)"></image>
      </swiper-item>
    </swiper>
    <!-- 商品信息 -->
    <view class="goods-info-box">
      <!-- 商品价格 -->
      <view class="price">¥{{ goodsDetailList.goods_price }}</view>
      <!-- 商品信息主体 -->
      <view class="goods-info-body">
        <!-- 商品名称 -->
        <view class="goods-name">{{ goodsDetailList.goods_name }}</view>
        <!-- 收藏 -->
        <view class="favi">
          <uni-icons type="star" size="18" color="gray"></uni-icons>
          <text>收藏</text>
        </view>
      </view>
      <!-- 运费 -->
      <view class="yf">快递: 免运费</view>
      <!-- 详情 -->
      <rich-text :nodes="goodsDetailList.goods_introduce"></rich-text>
      <view class="goods-carts">
        <uni-goods-nav :options="options" :fill="true" :button-group="buttonGroup" @click="onClick"
          @buttonClick="buttonClick" />
      </view>
    </view>
  </view>
</template>

<script setup>
import { ref } from 'vue'
import { onLoad } from '@dcloudio/uni-app'

const options = ref([ // 将 options 定义为响应式
  {
    icon: 'shop',
    text: '店铺',
    infoBackgroundColor: '#007aff',
    infoColor: "red"
  },
  {
    icon: 'cart',
    text: '购物车',
    info: 9
  }
]);

const buttonGroup = ref([ // 将 buttonGroup 定义为响应式
  {
    text: '加入购物车',
    backgroundColor: '#ff0000',
    color: '#fff'
  },
  {
    text: '立即购买',
    backgroundColor: '#ffa200',
    color: '#fff'
  }
]);

function onClick(e) {
  console.log(e); // 检查 e 的内容
  if(e.content.text === '购物车'){
    uni.switchTab({
      url: '/pages/cart/cart'
    });
  }
}

function buttonClick(e) {
  console.log(1111)
}


const goodsDetailList = ref([])

// 点击放大预览图片
function preview(index) {
  uni.previewImage({
    current: index,
    urls: goodsDetailList.value.pics.map(item => item.pics_big)
  })
}

// 首次加载
onLoad((option) => {
  // 接收传递过来的 goods_id
  const goods_id = option.goods_id
  // 调用获取详情页面的数据函数
  getGoodsDetailData(goods_id)
})

// 获取详情页面的数据
async function getGoodsDetailData(goods_id) {
  const res = await uni.$http.get('/api/public/v1/goods/detail', { goods_id })
  console.log(res)
  if (res.data.meta.status === 200) {
    res.data.message.goods_introduce =
      res.data.message.goods_introduce.replace(/<img/g, '<img style="display:block"').replace(/webp/g, 'jpg')
    goodsDetailList.value = res.data.message
  }
}
</script>

<style lang="scss" scoped>
// 轮播图开始
swiper {
  height: 750rpx;

  .swiper-image {
    width: 100%;
    height: 100%;
  }
}

// 轮播图结束

// 商品信息开始
.goods-info-box {
  padding: 10px;
  padding-right: 0;

  .price {
    color: #c00000;
    font-size: 18px;
    margin: 10px 0;
  }

  .goods-info-body {
    display: flex;
    justify-content: space-between;

    .goods-name {
      font-size: 14px;
      margin-right: 10px;
    }

    .favi {
      width: 120px;
      font-size: 12px;
      display: flex;
      flex-direction: column;
      align-items: center;
      border-left: 1px #eee solid;
      color: gray;
    }
  }

  .yf {
    font-size: 12px;
    color: gray;
    margin: 10px 0;
  }
}

// 购物车栏
.goods-carts {
		/* #ifndef APP-NVUE */
		display: flex;
		/* #endif */
		flex-direction: column;
		position: fixed;
		left: 0;
		right: 0;
		/* #ifdef H5 */
		left: var(--window-left);
		right: var(--window-right);
		/* #endif */
		bottom: 0;
	}
</style>
